﻿using System;
using System.Collections.Generic;

namespace UniRx.Operators
{
	// Token: 0x020002C6 RID: 710
	internal class ObserveOnObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06000F51 RID: 3921 RVA: 0x00047362 File Offset: 0x00045762
		public ObserveOnObservable(IObservable<T> source, IScheduler scheduler) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.scheduler = scheduler;
		}

		// Token: 0x06000F52 RID: 3922 RVA: 0x00047380 File Offset: 0x00045780
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			ISchedulerQueueing schedulerQueueing = this.scheduler as ISchedulerQueueing;
			if (schedulerQueueing == null)
			{
				return new ObserveOnObservable<T>.ObserveOn(this, observer, cancel).Run();
			}
			return new ObserveOnObservable<T>.ObserveOn_(this, schedulerQueueing, observer, cancel).Run();
		}

		// Token: 0x040008ED RID: 2285
		private readonly IObservable<T> source;

		// Token: 0x040008EE RID: 2286
		private readonly IScheduler scheduler;

		// Token: 0x020002C7 RID: 711
		private class ObserveOn : OperatorObserverBase<T, T>
		{
			// Token: 0x06000F53 RID: 3923 RVA: 0x000473BB File Offset: 0x000457BB
			public ObserveOn(ObserveOnObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000F54 RID: 3924 RVA: 0x000473D8 File Offset: 0x000457D8
			public IDisposable Run()
			{
				this.isDisposed = false;
				IDisposable disposable = this.parent.source.Subscribe(this);
				return StableCompositeDisposable.Create(disposable, Disposable.Create(delegate
				{
					object obj = this.scheduleDisposables;
					lock (obj)
					{
						this.isDisposed = true;
						foreach (IDisposable disposable2 in this.scheduleDisposables)
						{
							disposable2.Dispose();
						}
						this.scheduleDisposables.Clear();
					}
				}));
			}

			// Token: 0x06000F55 RID: 3925 RVA: 0x00047418 File Offset: 0x00045818
			public override void OnNext(T value)
			{
				SingleAssignmentDisposable self = new SingleAssignmentDisposable();
				object obj = this.scheduleDisposables;
				LinkedListNode<IDisposable> node;
				lock (obj)
				{
					if (this.isDisposed)
					{
						return;
					}
					node = this.scheduleDisposables.AddLast(self);
				}
				self.Disposable = this.parent.scheduler.Schedule(delegate()
				{
					self.Dispose();
					object obj2 = this.scheduleDisposables;
					lock (obj2)
					{
						if (node.List != null)
						{
							node.List.Remove(node);
						}
					}
					this.observer.OnNext(value);
				});
			}

			// Token: 0x06000F56 RID: 3926 RVA: 0x000474BC File Offset: 0x000458BC
			public override void OnError(Exception error)
			{
				SingleAssignmentDisposable self = new SingleAssignmentDisposable();
				object obj = this.scheduleDisposables;
				LinkedListNode<IDisposable> node;
				lock (obj)
				{
					if (this.isDisposed)
					{
						return;
					}
					node = this.scheduleDisposables.AddLast(self);
				}
				self.Disposable = this.parent.scheduler.Schedule(delegate()
				{
					self.Dispose();
					object obj2 = this.scheduleDisposables;
					lock (obj2)
					{
						if (node.List != null)
						{
							node.List.Remove(node);
						}
					}
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						this.Dispose();
					}
				});
			}

			// Token: 0x06000F57 RID: 3927 RVA: 0x00047560 File Offset: 0x00045960
			public override void OnCompleted()
			{
				SingleAssignmentDisposable self = new SingleAssignmentDisposable();
				object obj = this.scheduleDisposables;
				LinkedListNode<IDisposable> node;
				lock (obj)
				{
					if (this.isDisposed)
					{
						return;
					}
					node = this.scheduleDisposables.AddLast(self);
				}
				self.Disposable = this.parent.scheduler.Schedule(delegate()
				{
					self.Dispose();
					object obj2 = this.scheduleDisposables;
					lock (obj2)
					{
						if (node.List != null)
						{
							node.List.Remove(node);
						}
					}
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						this.Dispose();
					}
				});
			}

			// Token: 0x040008EF RID: 2287
			private readonly ObserveOnObservable<T> parent;

			// Token: 0x040008F0 RID: 2288
			private readonly LinkedList<IDisposable> scheduleDisposables = new LinkedList<IDisposable>();

			// Token: 0x040008F1 RID: 2289
			private bool isDisposed;
		}

		// Token: 0x020002C8 RID: 712
		private class ObserveOn_ : OperatorObserverBase<T, T>
		{
			// Token: 0x06000F59 RID: 3929 RVA: 0x0004786C File Offset: 0x00045C6C
			public ObserveOn_(ObserveOnObservable<T> parent, ISchedulerQueueing scheduler, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
				this.scheduler = scheduler;
				this.isDisposed = new BooleanDisposable();
				this.onNext = new Action<T>(this.OnNext_);
			}

			// Token: 0x06000F5A RID: 3930 RVA: 0x000478A4 File Offset: 0x00045CA4
			public IDisposable Run()
			{
				IDisposable disposable = this.parent.source.Subscribe(this);
				return StableCompositeDisposable.Create(disposable, this.isDisposed);
			}

			// Token: 0x06000F5B RID: 3931 RVA: 0x000478CF File Offset: 0x00045CCF
			private void OnNext_(T value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06000F5C RID: 3932 RVA: 0x000478E0 File Offset: 0x00045CE0
			private void OnError_(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000F5D RID: 3933 RVA: 0x00047918 File Offset: 0x00045D18
			private void OnCompleted_(Unit _)
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000F5E RID: 3934 RVA: 0x00047950 File Offset: 0x00045D50
			public override void OnNext(T value)
			{
				this.scheduler.ScheduleQueueing<T>(this.isDisposed, value, this.onNext);
			}

			// Token: 0x06000F5F RID: 3935 RVA: 0x0004796A File Offset: 0x00045D6A
			public override void OnError(Exception error)
			{
				this.scheduler.ScheduleQueueing<Exception>(this.isDisposed, error, new Action<Exception>(this.OnError_));
			}

			// Token: 0x06000F60 RID: 3936 RVA: 0x0004798A File Offset: 0x00045D8A
			public override void OnCompleted()
			{
				this.scheduler.ScheduleQueueing<Unit>(this.isDisposed, Unit.Default, new Action<Unit>(this.OnCompleted_));
			}

			// Token: 0x040008F2 RID: 2290
			private readonly ObserveOnObservable<T> parent;

			// Token: 0x040008F3 RID: 2291
			private readonly ISchedulerQueueing scheduler;

			// Token: 0x040008F4 RID: 2292
			private readonly BooleanDisposable isDisposed;

			// Token: 0x040008F5 RID: 2293
			private readonly Action<T> onNext;
		}
	}
}
